package com.kakarote.job.examine;

import cn.hutool.core.collection.CollectionUtil;
import com.kakarote.core.feign.admin.entity.AdminCompany;
import com.kakarote.core.feign.admin.service.AdminService;
import com.kakarote.core.feign.examine.service.ExamineService;
import com.kakarote.core.utils.UserUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 审核模块定时任务
 */
@Component
@Slf4j
public class ExamineJob {


    @Autowired
    private ExamineService examineService;

    @Autowired
    private AdminService adminService;

    @XxlJob("ExamineTimeOutJob")
    public ReturnT<String> examineTimeOutJobHandler(String param) {
        XxlJobLogger.log("任务超时调度-------审批自动提醒");
        List<AdminCompany> companyList = examineService.selectAllNoHandleLimitCompany().getData();
        if (CollectionUtil.isEmpty(companyList)) {
            return ReturnT.SUCCESS;
        }
        for (AdminCompany company : companyList) {
            try {
                Long userId = adminService.queryUserIdByUserNameAndCompanyId(company.getCompanyManage()).getData();
                if (userId != null) {
                    XxlJobLogger.log("企业名称:{},开始执行", company.getCompanyName());
                    examineService.queryTimeOutRecordList(userId);
                    XxlJobLogger.log("企业名称:{},结束执行", company.getCompanyName());
                }
            } catch (Exception ex) {
                log.error("执行错误", ex);
            } finally {
                UserUtil.removeUser();
            }
        }
        XxlJobLogger.log("任务超时调度-------审批自动提醒完成");
        return ReturnT.SUCCESS;
    }


    /**
     * 功能描述: <br>
     * 〈处理已生成审批节点但是审批人员异常〉
     *
     * @param:
     * @return: com.kakarote.core.common.Result<java.util.List < java.lang.Long>>
     * @throws:
     * @author: zyh
     * @date: 2022/10/18 14:33
     */
    @XxlJob("ExamineAbnormalJob")
    public ReturnT<String> handleExamineUserAbnormal(String param) {
        XxlJobLogger.log("审批任务异常调度-------审批自动提醒");
        List<AdminCompany> companyList = examineService.selectAllNoHandleAbnormalCompany().getData();
        for (AdminCompany company : companyList) {
            try {
                XxlJobLogger.log("企业名称:{},开始执行", company.getCompanyName());
                examineService.handleExamineUserAbnormal();
                XxlJobLogger.log("企业名称:{},结束执行", company.getCompanyName());
            } catch (Exception ex) {
                log.error("执行错误", ex);
            } finally {
                UserUtil.removeUser();
            }
        }
        XxlJobLogger.log("审批任务异常调度-------审批自动提醒完成");
        return ReturnT.SUCCESS;
    }

    /**
     * 流程委托在到达结束时间后会自动变更为“停用”状态
     */
    @XxlJob("ExamineDelegateJob")
    public ReturnT<String> examineDelegateJob(String param) {
        try {
            examineService.updateDelegateStatus();
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.error("执行错误", e);
            return ReturnT.FAIL;
        }
    }
}
